<script setup lang="ts">
import { nextTick, onMounted, onUnmounted, reactive, ref, shallowRef } from 'vue'
import type { Component, ShallowRef } from 'vue'
import { gpsbus } from '@/utils/gps/event'
import DeviceList from '@/views/dashboard/left/deviceList.vue'
import PlaybackList from '@/views/dashboard/left/playbackList.vue'
import TrackList from '@/views/dashboard/left/trackList.vue'
import Map from '@/views/dashboard/right/map.vue'
import MapTrackAndInfo from '@/views/dashboard/right/mapTrackAndInfo.vue'
import VideoAndMap from '@/views/dashboard/right/videoAndMap.vue'
import PlaybackAndMapTrack from '@/views/dashboard/right/playbackAndMapTrack.vue'
import { List, MapLocation, VideoCamera, VideoCameraFilled, Expand } from '@element-plus/icons-vue'
import useDeviceStore from '@/store/gps/device'
import { storeToRefs } from 'pinia'

const deviceStore = useDeviceStore()

type PageName = 'list' | 'track' | 'video' | 'playback'
type ComponentsList = {
  leftComponent: ShallowRef<Component>
  rightComponent: ShallowRef<Component>
}

const { video } = storeToRefs(deviceStore)
const activeName = ref<PageName>('list')
const currentLeftComponent = shallowRef<Component>(DeviceList)
const currentRightComponent = shallowRef<Component>(VideoAndMap)

const leftRef = ref()
const rightRef = ref()

const componentsList = reactive<{ [key in PageName]: ComponentsList }>({
  list: {
    leftComponent: shallowRef(DeviceList),
    rightComponent: shallowRef(VideoAndMap)
  },
  track: {
    leftComponent: shallowRef(TrackList),
    rightComponent: shallowRef(MapTrackAndInfo)
  },
  video: {
    leftComponent: shallowRef(DeviceList),
    rightComponent: shallowRef(VideoAndMap)
  },
  playback: {
    leftComponent: shallowRef(PlaybackList),
    rightComponent: shallowRef(PlaybackAndMapTrack)
  }
})

// * offSwitchMenu: 本组件内部方法 用于切换菜单
//       @switch-menu="offSwitchMenu"

onMounted(() => {
  gpsbus.on('switchMenu', offSwitchMenu)
})
onUnmounted(() => {
  gpsbus.off('switchMenu', offSwitchMenu)
})

/**
 * 切换页面
 */
function tabChange(name: PageName) {
  currentLeftComponent.value = componentsList[name].leftComponent
  currentRightComponent.value = componentsList[name].rightComponent
  video.value = name === 'video'
}

/**
 * 外部切换页面, 并可传参
 * @param name
 */
function offSwitchMenu(name: PageName) {
  activeName.value = name
  tabChange(name)
}

/**
 * 展开按钮
 */
function expandHandle() {
  const nav = document.querySelector('#home_nav')
  nav?.classList.remove('fold')
}
</script>
<script lang="ts">
export default {
  name: 'DashboardIndex'
}
</script>

<template>
  <section class="home">
    <nav id="home_nav">
      <div class="nav-content">
        <el-tabs v-model="activeName" tab-position="top" class="demo-tabs" @tab-change="tabChange">
          <el-tab-pane name="list">
            <template #label>
              <span class="custom-tabs-label">
                <el-icon><List /></el-icon>
                <span>监控</span>
              </span>
            </template>
          </el-tab-pane>
          <el-tab-pane name="track">
            <template #label>
              <span class="custom-tabs-label">
                <el-icon><MapLocation /></el-icon>
                <span>轨迹</span>
              </span>
            </template>
          </el-tab-pane>
<!--          <el-tab-pane name="video" disabled>-->
<!--            <template #label>-->
<!--              <span class="custom-tabs-label">-->
<!--                <el-icon><VideoCamera /></el-icon>-->
<!--                <span>视频</span>-->
<!--              </span>-->
<!--            </template>-->
<!--          </el-tab-pane>-->
          <el-tab-pane name="playback">
            <template #label>
              <span class="custom-tabs-label">
                <el-icon><VideoCameraFilled /></el-icon>
                <span>回放</span>
              </span>
            </template>
          </el-tab-pane>
        </el-tabs>
        <keep-alive>
          <component :is="currentLeftComponent" ref="leftRef"></component>
        </keep-alive>
      </div>
      <div class="small_control">
        <el-button circle @click="expandHandle">
          <el-icon><Expand /></el-icon>
        </el-button>
      </div>
    </nav>
    <keep-alive>
      <component :is="currentRightComponent" ref="rightRef" :type="activeName" class="adapt"></component>
    </keep-alive>
  </section>
</template>

<style scoped lang="css">
.adapt {
  padding: 0 10px 10px 0;
  box-sizing: border-box;
}
.adapt > *:first-child {
  height: 70%;
}
.adapt > *:last-child {
  height: 30%;
}
.home {
  display: flex;
  position: absolute;
  inset: 0;
  background-color: white;
}
.home > div {
  flex: 1;
}
nav {
  width: 350px;
  overflow: hidden;
  transition: width 0.5s;
}
.small_control {
  position: absolute;
  width: 50px;
  left: 0;
  top: 0;
  height: 100%;
  z-index: 100;
  transition: transform 0.5s;
  transform: translateX(-50px);
  background-color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 0;
}
.expand {
}
.fold {
  width: 50px;
}
.fold > .small_control {
  transform: translateX(0);
}

.nav-content {
  height: 100%;
  box-sizing: border-box;
  min-width: 350px;
  background-color: white;
  padding: 0 10px 10px 10px;
}
::v-deep(.demo-tabs .el-tabs__nav) {
  float: none;
}
::v-deep(.demo-tabs .el-tabs__item) {
  flex: 1;
}
.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
.demo-tabs .custom-tabs-label .el-icon {
  vertical-align: middle;
}
.demo-tabs .custom-tabs-label span {
  vertical-align: middle;
  margin-left: 4px;
}
</style>
